package com.controller;

import com.entity.HyOrder;

import com.service.OrderService;
import com.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @program: honye-fang
 * @description:
 * @author: Baiyyy
 * @create: 2025-02-14 21:59
 **/
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //创建订单
    @PostMapping("/createOrder/{userId}/{furnitureId}/{quantity}")
    public ResponseResult<HyOrder> createOrder(@PathVariable Long userId, @PathVariable Long furnitureId, @PathVariable Integer quantity) {
        try {
            HyOrder order = orderService.createOrder(userId, furnitureId, quantity);
            return ResponseResult.success(order);
        } catch (Exception e) {
            return ResponseResult.error("创建订单失败", e.getMessage());
        }
    }

    //根据订单id获取订单信息
    @GetMapping("/getOrderById/{orderid}")
    public ResponseResult<HyOrder> getOrderById(@PathVariable Long orderid) {
        try {
            HyOrder order = orderService.getOrderById(orderid);
            if (order == null) {
                return ResponseResult.error("订单不存在");
            }
            return ResponseResult.success(order);
        } catch (Exception e) {
            return ResponseResult.error("获取订单失败", e.getMessage());
        }
    }

    //根据用户id获取订单信息
    @GetMapping("/getOrderByUserId/{userid}")
    public ResponseResult<List<HyOrder>> getOrderByUserId( @PathVariable Long userid) {
        try {
            List<HyOrder> orders = orderService.getOrderByUserId(userid);
            if (orders == null || orders.isEmpty()) {
                return ResponseResult.error("用户没有订单");
            }
            return ResponseResult.success(orders);
        } catch (Exception e) {
            return ResponseResult.error("获取订单失败", e.getMessage());
        }
    }

    ///更新订单状态
    @PutMapping("/updateOrderStatus/{orderid}/{status}")
    public ResponseResult<Boolean> updateOrderStatus(@PathVariable Long orderid,
                                                     @PathVariable String status) {
        try {
            boolean result = orderService.updateOrderStatus(orderid, status);
            if (result) {
                return ResponseResult.success(true);
            } else {
                return ResponseResult.error("更新订单状态失败");
            }
        } catch (Exception e) {
            return ResponseResult.error("更新订单状态失败", e.getMessage());
        }
    }

    //定时取消订单
    @PutMapping("/cancelTimeoutOrders")
    public ResponseResult<Boolean> cancelTimeoutOrders() {
        try {
            orderService.checkAndCancelTimeoutOrders();
            return ResponseResult.success(true);
        } catch (Exception e) {
            return ResponseResult.error("取消定时订单失败", e.getMessage());
        }
    }



}
